"""
基础对话框类 - 统一的对话框样式
"""

import logging
from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
    QPushButton, QDialogButtonBox, QMessageBox, QLabel, QWidget
)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QFont

logger = logging.getLogger(__name__)

class BaseDialog(QDialog):
    """基础对话框类，提供统一的样式和布局"""
    
    # 信号定义
    data_saved = pyqtSignal(object)  # 数据保存信号
    
    def __init__(self, parent=None, title="对话框", width=600, height=400):
        super().__init__(parent)
        
        self.setWindowTitle(title)
        self.setModal(True)
        self.resize(width, height)
        
        # 设置统一样式
        self.setStyleSheet("""
            QDialog {
                background-color: #FFFFFF;
            }
            QLabel {
                color: #333333;
                font-size: 11px;
            }
            QPushButton {
                background-color: #2196F3;
                color: white;
                border: none;
                padding: 8px 16px;
                border-radius: 4px;
                font-size: 11px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #1976D2;
            }
            QPushButton:pressed {
                background-color: #0D47A1;
            }
            QPushButton:disabled {
                background-color: #CCCCCC;
                color: #666666;
            }
            QPushButton[type="cancel"] {
                background-color: #F5F5F5;
                color: #333333;
                border: 1px solid #DDDDDD;
            }
            QPushButton[type="cancel"]:hover {
                background-color: #EEEEEE;
            }
            QPushButton[type="cancel"]:pressed {
                background-color: #E0E0E0;
            }
        """)
        
        # 初始化UI
        self.init_ui()
    
    def init_ui(self):
        """初始化UI界面"""
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setContentsMargins(20, 20, 20, 20)
        self.main_layout.setSpacing(20)
        
        # 创建内容区域（子类实现）
        self.content_widget = self.create_content_widget()
        if self.content_widget:
            self.main_layout.addWidget(self.content_widget)
        
        # 创建按钮区域
        self.create_button_area()
    
    def create_content_widget(self):
        """创建内容区域 - 子类需要实现"""
        return QWidget()
    
    def create_button_area(self):
        """创建按钮区域"""
        button_layout = QHBoxLayout()
        button_layout.addStretch()
        
        # 确定按钮（左侧）
        self.ok_button = QPushButton("确定")
        self.ok_button.clicked.connect(self.accept_data)
        button_layout.addWidget(self.ok_button)
        
        # 取消按钮（右侧）
        self.cancel_button = QPushButton("取消")
        self.cancel_button.setProperty("type", "cancel")
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        # 设置默认按钮
        self.ok_button.setDefault(True)
        
        self.main_layout.addLayout(button_layout)
    
    def accept_data(self):
        """接受数据 - 子类需要实现"""
        self.accept()
    
    def show_error_message(self, title, message):
        """显示错误消息"""
        QMessageBox.critical(self, title, message)
    
    def show_success_message(self, title, message):
        """显示成功消息"""
        QMessageBox.information(self, title, message)
    
    def show_warning_message(self, title, message):
        """显示警告消息"""
        QMessageBox.warning(self, title, message)
    
    def show_question_message(self, title, message):
        """显示确认消息"""
        reply = QMessageBox.question(
            self, title, message,
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No
        )
        return reply == QMessageBox.Yes